package com.fwtai.service.web;

import com.fwtai.bean.PageFormData;
import com.fwtai.quartz.QuartzDelayed;
import com.fwtai.tool.IdBuilder;
import com.fwtai.tool.ToolClient;
import com.fwtai.web.MallOrderInfoDao;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * 积分商城的订单业务层
 * @作者 田应平
 * @版本 v1.0
 * @QQ号码 444141300
 * @Email service@fwtai.com
 * @创建日期 2024-02-20 14:14:19
 * @官网 <url>http://www.fwtai.com</url>
*/
@Service
public class MallOrderInfoService{

  private final MallOrderInfoDao mallOrderInfoDao;

  private final ThreadPoolTaskExecutor taskExecutor;

  public MallOrderInfoService(final MallOrderInfoDao mallOrderInfoDao,final ThreadPoolTaskExecutor taskExecutor){
    this.mallOrderInfoDao = mallOrderInfoDao;
    this.taskExecutor = taskExecutor;
  }

  public String addLogistics(final HttpServletRequest request){
    return CompletableFuture.supplyAsync(()->{
      final PageFormData formData = new PageFormData(request,14);
      final String p_out_trade_no = "out_trade_no";
      final String p_user_id = "user_id";
      final String p_openid = "openid";
      final String p_type = "type";
      final String p_transactionId = "transactionId";
      final String p_distributor_name = "distributor_name";
      final String p_waybill_id = "waybill_id";
      final String p_sender_name = "sender_name";
      final String p_sender_mobile = "sender_mobile";
      final String p_sender_province = "sender_province";
      final String p_sender_city = "sender_city";
      final String p_sender_area = "sender_area";
      final String p_sender_address = "sender_address";
      final String validate = ToolClient.validateField(formData,p_out_trade_no,p_user_id,p_openid,p_distributor_name,p_waybill_id,p_sender_name,p_sender_mobile,p_sender_address,p_type);
      if(validate != null) return validate;
      final String len_distributor_name = formData.getString(p_distributor_name);
      if(len_distributor_name != null && len_distributor_name.length() > 128){
        return ToolClient.createJsonFail("物流公司名称字数太多");
      }
      final String len_waybill_id = formData.getString(p_waybill_id);
      if(len_waybill_id != null && len_waybill_id.length() > 128){
        return ToolClient.createJsonFail("运单号字数太多");
      }
      final String len_sender_name = formData.getString(p_sender_name);
      if(len_sender_name != null && len_sender_name.length() > 128){
        return ToolClient.createJsonFail("发件人姓名字数太多");
      }
      final String len_sender_mobile = formData.getString(p_sender_mobile);
      if(len_sender_mobile != null && len_sender_mobile.length() > 20){
        return ToolClient.createJsonFail("发件人手机号字数太多");
      }
      final String len_sender_province = formData.getString(p_sender_province);
      if(len_sender_province != null && len_sender_province.length() > 64){
        return ToolClient.createJsonFail("省名称字数太多");
      }
      final String len_sender_city = formData.getString(p_sender_city);
      if(len_sender_city != null && len_sender_city.length() > 128){
        return ToolClient.createJsonFail("市名称字数太多");
      }
      final String len_sender_area = formData.getString(p_sender_area);
      if(len_sender_area != null && len_sender_area.length() > 128){
        return ToolClient.createJsonFail("区县名称字数太多");
      }
      final String len_sender_address = formData.getString(p_sender_address);
      if(len_sender_address != null && len_sender_address.length() > 128){
        return ToolClient.createJsonFail("发件人详细地址字数太多");
      }
      formData.put("kid",new IdBuilder().getId121L());
      final int rows = mallOrderInfoDao.addLogistics(formData,formData.getLong(p_out_trade_no));
      final Integer type = formData.getInteger(p_type);
      if(type != null && type == 1){
        if(rows > 0){
          QuartzDelayed.editDelayedResult(formData.getString(p_transactionId),formData.getString(p_openid),null);
        }
      }
      return ToolClient.executeRows(rows);
    },taskExecutor).join();
  }

  public String editLogistics(final HttpServletRequest request){
    return CompletableFuture.supplyAsync(()->{
      final PageFormData formData = new PageFormData(request,10);
      final String p_distributor_name = "distributor_name";
      final String p_waybill_id = "waybill_id";
      final String p_sender_name = "sender_name";
      final String p_sender_mobile = "sender_mobile";
      final String p_sender_province = "sender_province";
      final String p_sender_city = "sender_city";
      final String p_sender_area = "sender_area";
      final String p_sender_address = "sender_address";
      final String p_out_trade_no = "out_trade_no";
      final String validate = ToolClient.validateField(formData,p_out_trade_no,p_distributor_name,p_waybill_id,p_sender_name,p_sender_mobile,p_sender_address);
      if(validate != null) return validate;
      final String len_distributor_name = formData.getString(p_distributor_name);
      if(len_distributor_name != null && len_distributor_name.length() > 128){
        return ToolClient.createJsonFail("物流公司名称字数太多");
      }
      final String len_waybill_id = formData.getString(p_waybill_id);
      if(len_waybill_id != null && len_waybill_id.length() > 128){
        return ToolClient.createJsonFail("运单号字数太多");
      }
      final String len_sender_name = formData.getString(p_sender_name);
      if(len_sender_name != null && len_sender_name.length() > 128){
        return ToolClient.createJsonFail("发件人姓名字数太多");
      }
      final String len_sender_mobile = formData.getString(p_sender_mobile);
      if(len_sender_mobile != null && len_sender_mobile.length() > 20){
        return ToolClient.createJsonFail("发件人手机号字数太多");
      }
      final String len_sender_province = formData.getString(p_sender_province);
      if(len_sender_province != null && len_sender_province.length() > 64){
        return ToolClient.createJsonFail("省名称字数太多");
      }
      final String len_sender_city = formData.getString(p_sender_city);
      if(len_sender_city != null && len_sender_city.length() > 128){
        return ToolClient.createJsonFail("市名称字数太多");
      }
      final String len_sender_area = formData.getString(p_sender_area);
      if(len_sender_area != null && len_sender_area.length() > 128){
        return ToolClient.createJsonFail("区县名称字数太多");
      }
      final String len_sender_address = formData.getString(p_sender_address);
      if(len_sender_address != null && len_sender_address.length() > 128){
        return ToolClient.createJsonFail("发件人详细地址字数太多");
      }
      return ToolClient.executeRows(mallOrderInfoDao.editLogistics(formData));
    },taskExecutor).join();
  }

  public String queryById(final Long out_trade_no){
    return CompletableFuture.supplyAsync(()->{
      if(out_trade_no == null){
        return ToolClient.jsonValidateField();
      }
      return ToolClient.queryJson(mallOrderInfoDao.queryById(out_trade_no));
    },taskExecutor).join();
  }

  public String listData(final PageFormData formData){
    return CompletableFuture.supplyAsync(()->{
      try{
        if(ToolClient.dataTableMysql(formData) == null) return ToolClient.jsonValidateField();
        final CompletableFuture<List<HashMap<String,Object>>> listData = CompletableFuture.supplyAsync(() -> mallOrderInfoDao.getListData(formData),taskExecutor);
        final CompletableFuture<Integer> listTotal = CompletableFuture.supplyAsync(() -> mallOrderInfoDao.getListTotal(formData),taskExecutor);
        return ToolClient.dataTableOK(listData.join(),listTotal.join());
      } catch (final Exception ignored){
        return ToolClient.dataTableException();
      }
    },taskExecutor).join();
  }
}